<?php
/**
 * Created by PhpStorm.
 * User: xp
 * Date: 2021/4/15
 * Time: 14:13
 */

namespace app\model\cron;



use app\model\BaseModel;
use support\Db;

class CronTaskLog extends BaseModel
{
    protected $table = 'video_cron_task_log';

    protected $primaryKey = 'id';

    public $timestamps = false;

    protected static $instance;

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    /**
     * 记录任务日记
     * @param string $cronConfig
     * @param string $result
     * @param string $ttls
     * @return mixed
     */
    public function insertTaskLog($cronConfig, $result, $ttls)
    {
        try{
            Db::connection('mysql')->beginTransaction();
            if(isset($cronConfig['cronid'])){
                $cronTask = CronTask::getInstance()->find($cronConfig['cronid']);
                if($cronTask==false){
                    return false;
                }

                isset($result['data']) &&  $result['data'] = is_array($result['data']) ? $result['data'] : (array) $result['data'];
                !isset($result['data']) && $result['data'] = [];

                $data = [
                    'cronid'=>$cronTask['cronid']??'',
                    'cron_expression'=>$cronTask['cron_expression'],
                    'cmd'=>$cronTask['cmd']??'',
                    'param'=>$cronTask['param']??'',
                    'spend_time'=>$ttls,
                    'result'=>isset($result['msg']) && $result['msg']=='success' ? 'success' : 'fail',
                ];

                $id = $this->insertGetId($data);

                !empty($result['data']) && is_array($result['data']) && $result['data'] = json_encode($result['data'],256);
                $insertData = [
                    'task_log_id'=>$id,
                    'cmd'=>$cronTask['cmd'],
                    'return_content'=>!empty($result['data']) ? $result['data'] : ($result['msg']??''),
                ];
                CronTaskLogContent::getInstance()->insertGetId($insertData);
            }

            Db::connection('mysql')->commit();
        }catch (\Exception $e){
            Db::connection('mysql')->rollBack();
            throw $e;
        }catch (\Throwable $e){
            Db::connection('mysql')->rollBack();
            throw $e;
        }

        return true;
    }

    public function getLogList($param)
    {
        $where = [
            ['tl.cronid','>',0]
        ];

        if(!empty($param['appid'])){
            $where[] = ['t.appid','=',$param['appid']];
        }
        if(!empty($param['cate_name'])){
            $where[] = ['t.cate_name','like',"%{$param['cate_name']}%"];
        }

        $query = $this->setTable($this->getTable().' as tl')->where($where)
            ->select(['tl.*','t.cate_name','t.appid','tlc.return_content'])
            ->join(CronTask::getInstance()->getTable().' as t','t.cronid','tl.cronid')
            ->join(CronTaskLogContent::getInstance()->getTable().' as tlc','tlc.task_log_id','tl.id');

         if(!empty($param['createtime'])){
             $query->whereBetween('tl.createtime',$param['createtime']);
         }

        $data = $this->buildPage($query);
        $data['data'] = $query->orderBy('tl.createtime','desc')->get();


        return $data;
    }
}